#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

int main(int argc, char** argv) {
	int numNodes,arity;
	srand(time(NULL));
    if (argc < 2) {
        fprintf(stderr, "no file specified\n");
        exit(EXIT_FAILURE);
    }
    int opt = -1;
    char* filename = (char*) malloc(50 * sizeof (char));
    while ((opt = getopt(argc, argv, "f:n:")) != -1) {
        //   printf("opt: %c\n", opt);
        switch (opt) {
            case 'f':
                strcpy(filename, optarg);
                break;
			case 'n':
				numNodes = atoi(optarg);
				arity = 1;
				int k = numNodes;
				while(k >= 10){
					arity++;
					k /= 10;
				}
				break;
			default:
				fprintf(stderr, "no file specified \n");
			}
		}
		printf("building graph with %d nodes\n",numNodes);
		FILE* file;
		file = fopen(filename,"a+");
		int size = 2*numNodes*(numNodes+1)+3;
		char* matrix = (char*)malloc(size*sizeof(char));
		char* cp = matrix;
		sprintf(cp,"%d",numNodes);
		cp += arity;
		*(cp++) = '\n';
		int i,j;
		for(j=0;j<numNodes;j++){
			for(i=0;i<numNodes;i++){
				*(cp++) = (rand()%2 == 1) ? '1' : '0';
				if(i+1 == numNodes) 
					*(cp++) = '\n';
				else			
					*(cp++) = ' ';
		}
	}
		*(cp++) = '\0';
		fputs(matrix,file);
		free(matrix);
		fclose(file);
		return 0;
	}